package com.cgbl.service.ui

import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.AddTeamMemberAdapter
import com.cgbl.service.adapter.AddTeamTagAdapter
import com.cgbl.service.databinding.AppActivitySelectMemberBinding
import com.cgbl.service.vm.SelectMemberViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.MemberAdapterBean
import com.robot.base.util.LiveDataEventManager

@Route(path = AppConstants.Router.Project.A_SELECT_MEMBER)
class SelectMemberActivity :
    GLBaseActivity<AppActivitySelectMemberBinding, SelectMemberViewModel>() {

    lateinit var addTeamTagAdapter: AddTeamMemberAdapter

    var selectedIds = arrayListOf<String>()

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_select_member

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        addTeamTagAdapter = AddTeamMemberAdapter(true)
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = addTeamTagAdapter
        }
        viewModel.fetchHomeMember()
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        val rightView = findViewById<TextView>(R.id.tv_right_text)
        titleView?.text = "选择人员"
        backView?.setOnClickListener { finish() }
        rightView.setOnClickListener {
            LiveDataEventManager.postSelectTagMemberEvent(addTeamTagAdapter.getSelectedMember())
            finish()
        }
    }

    override fun initViewObservable() {
        viewModel.uc.teamMemberDataEvent.observe(this) {
            addTeamTagAdapter.setNewInstance(it as MutableList<MemberAdapterBean>?)
            addTeamTagAdapter.setDefaultCheck(selectedIds)
        }
    }

    override fun initParam() {
        selectedIds = intent.getStringArrayListExtra(AppConstants.BundleKey.IDS) ?: arrayListOf()
    }
}